package com.lifeng.finance.demo.controller;
import com.lifeng.finance.demo.entity.Project;
import com.lifeng.finance.demo.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@ComponentScan(basePackages = {"com.lifeng.finance.demo.service.*"})
@RestController
public class ProjectController {


    @Autowired
    private ProjectService projectService;


    @RequestMapping(path = "/project/by_id")
    public Project findOneById(@RequestParam(name = "id") int id) {
        return projectService.findOneById(id);
    }

    @RequestMapping(path = "/project/by_name")
    public List<Project> findSomeByName(@RequestParam(name = "name") String name) {
        return projectService.findSomeByName(name);
    }

    @RequestMapping(path = "/project/by_customer")
    public List<Project> findSomeByName(@RequestParam(name = "customer_id") int customerId) {
        return projectService.findSomeByCustomerId(customerId);
    }

    @RequestMapping(path = "/project/add")
    public Project add(@RequestParam(name = "customer_id") int projectId, @RequestParam(name = "name") String name) {
        Project project = new Project(projectId, name);
        projectService.add(project);
        return project;
    }

    @RequestMapping(path = "/project/update")
    public Project update(@RequestParam(name = "id") int id, @RequestParam(name = "name") String name) {

        Project project = projectService.findOneById(id);
        project.setName(name);
        projectService.update(project);
        return project;
    }

}
